import { addQuestionBank } from "@/api/question-bank"
import { ProColumns, ProTable } from "@ant-design/pro-components"
import { message, Modal } from "antd"
import React from "react"

interface Props {
  visible: boolean
  columns: ProColumns<QuestionBank>[]
  onSubmit: (values: QuestionBankAddRequest) => void
  onCancel: () => void
}

/**
 * 添加节点
 * @param fields
 */
const handleAdd = async (fields: QuestionBankAddRequest) => {
  const hide = message.loading("正在添加")
  try {
    await addQuestionBank(fields)
    hide()
    message.success("创建成功")
    return true
  } catch (error) {
    hide()
    error instanceof Error && message.error("创建失败，" + error.message)
    return false
  }
}

/**
 * 创建弹窗
 * @param props
 * @constructor
 */
const CreateModal: React.FC<Props> = (props) => {
  const { visible, columns, onSubmit, onCancel } = props

  return (
    <Modal
      destroyOnClose
      title={"创建"}
      open={visible}
      footer={null}
      onCancel={() => {
        onCancel?.()
      }}
    >
      <ProTable
        type="form"
        columns={columns}
        onSubmit={async (values: QuestionBankAddRequest) => {
          const success = await handleAdd(values)
          if (success) {
            onSubmit?.(values)
          }
        }}
      />
    </Modal>
  )
}
export default CreateModal
